package com.JFapp.annotation.validator;

import com.JFapp.annotation.validator.name.UniqueName;

import com.JFapp.domain.entity.User;
import com.JFapp.service.UserService;
import com.JFapp.utils.SecurityUtils;
import com.alibaba.excel.util.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


public class UniqueNameValidator implements ConstraintValidator<UniqueName,String> {
    @Autowired
    private UserService userService;
    @Override
    public boolean isValid(String name, ConstraintValidatorContext context) {

            if (StringUtils.isBlank(name)) {
                return true;
            }
            User user = userService.getByName(name);

            if ( user== null||user.getId()==SecurityUtils.getUserId()) {
                return true;
            }
            return  false;
        }
    }

